package com.jrtc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.config.constants.PublicConstants;
import com.jrtc.base.entity.bo.*;
import com.jrtc.base.entity.vo.*;
import com.jrtc.dao.*;
import com.jrtc.entity.vo.*;
import com.jrtc.entity.vo.ExercisePrescriptionVo;
import com.jrtc.entity.vo.PrescriptionVo;
import com.jrtc.service.DoctorService;
import com.jrtc.service.PatientExerciseprescriptionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 患者运动处方表(PatientExerciseprescription)表服务实现类
 *
 * @author makejava
 * @since 2022-03-21 17:34:12
 */
@Service("patientExerciseprescriptionService")
public class PatientExerciseprescriptionServiceImpl implements PatientExerciseprescriptionService
{
    @Resource
    private PatientExerciseprescriptionDao patientExerciseprescriptionDao;

    @Resource
    private ExerciseprescriptionDao exerciseprescriptionDao;

    @Resource
    private DiseaseDao diseaseDao;

    @Resource
    private PublicVideoDao publicVideoDao;

    @Resource
    private DoctorDao doctorDao;
    @Resource
    private PrescriptionVideoDao prescriptionVideoDao;
    @Resource
    private SysDictItemDao sysDictItemDao;
    @Resource
    private SysDictDao sysDictDao;
    @Resource
    private DoctorService doctorService;
    @Resource
    private HospitalDao hospitalDao;
    @Resource
    private PrescriptionLabelOneDao prescriptionLabelOneDao;
    @Resource
    private PrescriptionLabelTwoDao prescriptionLabelTwoDao;
    @Resource
    private PrescriptionLabelThreeDao prescriptionLabelThreeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param favouriteId 主键
     * @return 实例对象
     */
    @Override
    public PatientExerciseprescription queryById(Integer favouriteId) {
        return this.patientExerciseprescriptionDao.queryById(favouriteId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<PatientExerciseprescription> queryAllByLimit(int offset, int limit) {
        return this.patientExerciseprescriptionDao.queryAllByLimit(offset, limit);
    }

    @Override
    public IPage<PatientExerciseprescription> queryPatientExerciseprescriptionList(PatientExerciseprescription patientExerciseprescription)
    {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<PatientExerciseprescription> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(patientExerciseprescription.getPageNo(), patientExerciseprescription.getPageSize());
        IPage<PatientExerciseprescription> patientExerciseprescriptionPage = this.patientExerciseprescriptionDao.queryPatientExerciseprescriptionList(page, patientExerciseprescription);
        return patientExerciseprescriptionPage;
    }
    /**
     * 新增数据
     *
     * @param patientExerciseprescription 实例对象
     * @return 实例对象
     */
    @Override
    public PatientExerciseprescription insert(PatientExerciseprescription patientExerciseprescription) {
        this.patientExerciseprescriptionDao.insert(patientExerciseprescription);
        return patientExerciseprescription;
    }

    /**
     * 修改数据
     *
     * @param patientExerciseprescription 实例对象
     * @return 实例对象
     */
    @Override
    public PatientExerciseprescription update(PatientExerciseprescription patientExerciseprescription) {
        this.patientExerciseprescriptionDao.update(patientExerciseprescription);
        return this.queryById(patientExerciseprescription.getFavouriteId());
    }

    /**
     * 通过主键删除数据
     *
     * @param favouriteId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer favouriteId) {
        return this.patientExerciseprescriptionDao.deleteById(favouriteId) > 0;
    }

    /**
     * 查询患者的运动处方记录
     * */
    @Override
    public List<PatientExerciseprescriptionVo> selectPatientExerciseprescriptionList(Integer patientId) throws Exception {
        if (patientId != null && patientId != 0){
            List<PatientExerciseprescription> patientExerciseprescriptions = patientExerciseprescriptionDao.selectPatientExerciseprescriptionList(patientId);
            if (patientExerciseprescriptions != null && patientExerciseprescriptions.size() > 0){
                List<PatientExerciseprescriptionVo> patientExerciseprescriptionVoList = new ArrayList<>();
                for (PatientExerciseprescription patientExerciseprescription : patientExerciseprescriptions) {
                    Exerciseprescription exerciseprescription = exerciseprescriptionDao.selectExerciseprescription(patientExerciseprescription.getPrescriptionId(), null);
                    if (exerciseprescription != null){
                        PatientExerciseprescriptionVo patientExerciseprescriptionVo = new PatientExerciseprescriptionVo();

                        List<PublicVideo> publicVideos = publicVideoDao.selectPrescriptionvideo(exerciseprescription.getPrescriptionId());
                        List<String> prescriptionvideoList = new ArrayList<>();
                        if (publicVideos != null && publicVideos.size() > 0){
                            for (PublicVideo publicVideo : publicVideos) {
                                prescriptionvideoList.add(publicVideo.getPrescriptionVideoUrl());
                            }
                        }

                        patientExerciseprescriptionVo.setPrescriptionVideo(prescriptionvideoList);
                        patientExerciseprescriptionVo.setPrescriptionName(exerciseprescription.getPrescriptionName());
                        patientExerciseprescriptionVo.setExerciseTime(exerciseprescription.getExerciseTime());
                        patientExerciseprescriptionVo.setGrade(exerciseprescription.getExerciseLevel());
                        if (exerciseprescription.getPrice() != null && !exerciseprescription.getPrice().equals("")){
                            patientExerciseprescriptionVo.setPrice(Integer.parseInt(exerciseprescription.getPrice()));
                        }
                        else {
                            patientExerciseprescriptionVo.setPrice(0);
                        }
                        patientExerciseprescriptionVoList.add(patientExerciseprescriptionVo);
                    }
                    else {
                        throw new Exception("未查到运动处方信息");
                    }
                }
                return patientExerciseprescriptionVoList;
            }
            else {
                throw new Exception("未查到患者运动处方记录信息");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 创建患者运动处方信息
     * */
    @Override
    public int insertPatientExerciseprescription(Integer prescriptionId, Integer patientId) throws Exception {
        if (patientId != null && patientId != 0 && prescriptionId != null && prescriptionId != 0){
            PatientExerciseprescription patientExerciseprescription = new PatientExerciseprescription();
            patientExerciseprescription.setPrescriptionId(prescriptionId);
            Exerciseprescription exerciseprescription = exerciseprescriptionDao.selectExerciseprescription(prescriptionId, null);
            if (exerciseprescription != null){
                patientExerciseprescription.setPrescriptionName(exerciseprescription.getPrescriptionName());
            }
            else {
                throw new Exception("运动处方id错误");
            }
            patientExerciseprescription.setPatientId(patientId);
            patientExerciseprescription.setIsfavourite("false");
            int i = patientExerciseprescriptionDao.insertPatientExerciseprescription(patientExerciseprescription);
            if (i > 0){
                return i;
            }
            else {
                throw new Exception("创建失败");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 患者所有运动处方信息列表
     * */
    @Override
    public ExercisePrescriptionVo allExercisePrescriptions(Integer patientId, String name) throws Exception {
        if (patientId != null && patientId != 0){
            ExercisePrescriptionVo exercisePrescriptionVo = new ExercisePrescriptionVo();
            List<PatientExerciseprescription> patientExerciseprescriptionList = patientExerciseprescriptionDao.allExercisePrescriptions(patientId, name);

            List<Integer> prescriptionIdList = new ArrayList<>();
            if (patientExerciseprescriptionList != null && patientExerciseprescriptionList.size() > 0){
                exercisePrescriptionVo.setMyPrescriptionNum(patientExerciseprescriptionList.size());//我的运动处方数量

                for (PatientExerciseprescription patientExerciseprescription : patientExerciseprescriptionList) {
                    prescriptionIdList.add(patientExerciseprescription.getPrescriptionId());
                }
            }
            else {
                exercisePrescriptionVo.setMyPrescriptionNum(0);//我的运动处方数量
            }

            HashSet hashSet = new HashSet(prescriptionIdList);
            prescriptionIdList.clear();
            prescriptionIdList.addAll(hashSet);

            List<Exerciseprescription> exerciseprescriptions;
            if (prescriptionIdList != null && prescriptionIdList.size() > 0){
                exerciseprescriptions = exerciseprescriptionDao.otherPrescriptionsNum(prescriptionIdList);
            }
            else {
                exerciseprescriptions = exerciseprescriptionDao.otherPrescriptions();
            }

            if (exerciseprescriptions != null && exerciseprescriptions.size() > 0){
                exercisePrescriptionVo.setOtherPrescriptionNum(exerciseprescriptions.size());//其他运动处方数量
            }
            else {
                exercisePrescriptionVo.setOtherPrescriptionNum(0);//其他运动处方数量
            }

            List<Disease> diseases = diseaseDao.selectDiseaseList();
            if (diseases != null && diseases.size() > 0){
                exercisePrescriptionVo.setPrescriptionNum(diseases.size());//运动处方类别数量
            }
            else {
                exercisePrescriptionVo.setPrescriptionNum(0);//运动处方类别数量
            }

            List<ExercisePrescriptionListVo> exercisePrescriptionListVoList = new ArrayList<>();
            List<Exerciseprescription> exerciseprescriptionList = exerciseprescriptionDao.selectExerciseprescriptionList(prescriptionIdList);
            ExercisePrescriptionListVo exercisePrescriptionListVo = exercisePrescriptionListVo(exerciseprescriptionList);
            exercisePrescriptionListVo.setPrescriptionName("我的运动处方");
            exercisePrescriptionListVoList.add(exercisePrescriptionListVo);

            ExercisePrescriptionListVo prescriptionListVo = exercisePrescriptionListVo(exerciseprescriptions);
            prescriptionListVo.setPrescriptionName("其他运动处方");
            exercisePrescriptionListVoList.add(prescriptionListVo);
            exercisePrescriptionVo.setExercisePrescriptionListVo(exercisePrescriptionListVoList);//运动处方总集合

            return exercisePrescriptionVo;
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 患者收藏的运动处方
     * */
    @Override
    public List<PatientCollectVo> selectPatientCollect(Integer patientId) throws Exception {
        if (patientId != null && patientId != 0){
            List<PatientExerciseprescription> patientExerciseprescriptionList = patientExerciseprescriptionDao.selectPatientCollect(patientId);
            if (patientExerciseprescriptionList != null && patientExerciseprescriptionList.size() > 0){
                List<PatientCollectVo> patientCollectVoList = new ArrayList<>();
                for (PatientExerciseprescription patientExerciseprescription : patientExerciseprescriptionList) {
                    PatientCollectVo patientCollectVo = new PatientCollectVo();
                    patientCollectVo.setPrescriptionId(patientExerciseprescription.getPrescriptionId());
                    patientCollectVo.setPrescriptionName(patientExerciseprescription.getPrescriptionName());
                    patientCollectVo.setExerciseStrength(patientExerciseprescription.getExerciseprescription().getExerciseLevel());
                    patientCollectVo.setExerciseTime(patientExerciseprescription.getExerciseprescription().getExerciseTime());
                    patientCollectVo.setExercisePictureUrl(patientExerciseprescription.getExerciseprescription().getExercisePictureUrl());

                    if (patientExerciseprescription.getDoctorId() != null && patientExerciseprescription.getDoctorId() != 0){
                        Doctor doctor = doctorDao.selectDoctorById(patientExerciseprescription.getDoctorId());
                        if (doctor != null){
                            throw new Exception("未查到医生信息");
                        }
                        patientCollectVo.setDoctorName(doctor.getDoctorName());
                    }

                    patientCollectVoList.add(patientCollectVo);
                }
                return patientCollectVoList;
            }
            else {
                throw new Exception("该患者还没有收藏运动处方");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 患者运动处方列表
     * */
    @Override
    public List<com.jrtc.base.entity.vo.PrescriptionVo> patientExercisePrescription(Integer id, String name) throws Exception {
        List<com.jrtc.base.entity.vo.PrescriptionVo> prescriptionVoList = new ArrayList<>();

        List<Integer> prescriptionId = new ArrayList<>();
        if (name != null){
            InformationVo informationVo = new InformationVo();
            informationVo.setName(name);
            List<Exerciseprescription> exerciseprescriptionList = exerciseprescriptionDao.selectExerciseprescriptions(informationVo);
            if (exerciseprescriptionList != null && exerciseprescriptionList.size() > 0){
                for (Exerciseprescription exerciseprescription : exerciseprescriptionList) {
                    prescriptionId.add(exerciseprescription.getPrescriptionId());
                }
            }
        }

        if (prescriptionId.size() <= 0){
            prescriptionId = null;
        }

        List<PatientExerciseprescription> patientExerciseprescriptionList = patientExerciseprescriptionDao.patientExercisePrescription(id, prescriptionId);
        for (PatientExerciseprescription patientExerciseprescription : patientExerciseprescriptionList) {
//            Exerciseprescription exerciseprescription = exerciseprescriptionDao.selectExerciseprescription(patientExerciseprescription.getPrescriptionId(), null);
//            if (exerciseprescription != null){
//                com.jrtc.base.entity.vo.PrescriptionVo prescriptionVo = new com.jrtc.base.entity.vo.PrescriptionVo();
//                prescriptionVo.setExerciseprescriptionId(exerciseprescription.getPrescriptionId());
//                prescriptionVo.setUrl(exerciseprescription.getExercisePictureUrl());
//                prescriptionVo.setExerciseprescriptionName(exerciseprescription.getPrescriptionName());
//                prescriptionVo.setGrade(exerciseprescription.getExerciseLevel());
//                prescriptionVo.setPlan(exerciseprescription.getExerciseCourse().toString());
//                prescriptionVo.setDuration(exerciseprescription.getExerciseTime().toString());
//                prescriptionVo.setPrice(exerciseprescription.getPrice());
//                prescriptionVoList.add(prescriptionVo);
//            }

            com.jrtc.base.entity.vo.PrescriptionVo prescriptionVo = new com.jrtc.base.entity.vo.PrescriptionVo();
            prescriptionVo.setExerciseprescriptionId(patientExerciseprescription.getFavouriteId());
            prescriptionVo.setUrl(patientExerciseprescription.getCover());
            prescriptionVo.setExerciseprescriptionName(patientExerciseprescription.getPrescriptionName());
            prescriptionVo.setGrade(patientExerciseprescription.getActivityIntensity());

            if (patientExerciseprescription.getRecoveryCycle() != null){
                String[] split = patientExerciseprescription.getRecoveryCycle().split("~");
                int time;
                try {
                    time = time(split[0], split[1]);
                } catch (Exception e){
                    throw new Exception(e.getMessage());
                }
                prescriptionVo.setPlan(String.valueOf(time));
            }
            prescriptionVoList.add(prescriptionVo);
        }
        return prescriptionVoList;
    }

    /**
     * 定制患者运动处方
     * */
    @Transactional
    @Override
    public int customMade(PatientExerciseprescription patientExerciseprescription, HttpServletRequest request) throws Exception {
        Doctor loginDoctor;
        try {
            loginDoctor = doctorService.getLoginDoctor(request);
        } catch (Exception e){
            throw new Exception(e.getMessage());
        }

        if (loginDoctor == null){
            throw new Exception("登录已过期");
        }

        patientExerciseprescription.setDoctorId(loginDoctor.getDoctorId());

        int insert = patientExerciseprescriptionDao.insert(patientExerciseprescription);

        if (patientExerciseprescription.getPurposeList() != null){
            int i = insertPrescriptionLabelOne(patientExerciseprescription.getPurposeList(), patientExerciseprescription.getFavouriteId());
            if (i <= 0){
                throw new Exception("添加目的标签失败");
            }
        }

        if (patientExerciseprescription.getRiskLevelList() != null){
            int i = insertPrescriptionLabelOne(patientExerciseprescription.getRiskLevelList(), patientExerciseprescription.getFavouriteId());
            if (i <= 0){
                throw new Exception("添加风险等级标签失败");
            }
        }

        if (patientExerciseprescription.getComplicationList() != null){
            int i = insertPrescriptionLabelOne(patientExerciseprescription.getComplicationList(), patientExerciseprescription.getFavouriteId());
            if (i <= 0){
                throw new Exception("添加并发症标签失败");
            }
        }

        if (patientExerciseprescription.getMuscleSelectionList() != null){
            int i = insertPrescriptionLabelOne(patientExerciseprescription.getMuscleSelectionList(), patientExerciseprescription.getFavouriteId());
            if (i <= 0){
                throw new Exception("添加肌肉选择标签失败");
            }
        }

        if (patientExerciseprescription.getApplicableDiseaseList() != null){
            int i = insertPrescriptionLabelOne(patientExerciseprescription.getApplicableDiseaseList(), patientExerciseprescription.getFavouriteId());
            if (i <= 0){
                throw new Exception("添加适用症标签失败");
            }
        }

        if (patientExerciseprescription.getPrescriptionVideoList() != null && patientExerciseprescription.getPrescriptionVideoList().size() > 0){
            for (PrescriptionVideo prescriptionVideo : patientExerciseprescription.getPrescriptionVideoList()) {
                PublicVideo publicVideo = publicVideoDao.selectPublicVideo(prescriptionVideo.getPublicVideoId());
                if (publicVideo != null){
                    prescriptionVideo.setPublicVideoName(publicVideo.getPrescriptionVideoName());
                    prescriptionVideo.setPublicVideoUrl(publicVideo.getPrescriptionVideoUrl());
                    prescriptionVideo.setTime(publicVideo.getTime());
                    prescriptionVideo.setBranch(publicVideo.getBranch());
                    prescriptionVideo.setSecond(publicVideo.getSecond());
                }

                prescriptionVideo.setHealthplanId(patientExerciseprescription.getFavouriteId());
                prescriptionVideo.setClassification("patient");
                insert = prescriptionVideoDao.insert(prescriptionVideo);
                if (insert <= 0){
                    throw new Exception("添加视频失败：" + prescriptionVideo.getPublicVideoId());
                }
            }
        }
        return insert;
    }

    /**
     * 查询待修改的患者运动处方
     * */
    @Override
    public PatientExerciseprescription selCustomMade(Integer id) {
        PatientExerciseprescription patientExerciseprescription = patientExerciseprescriptionDao.queryById(id);

        List<PrescriptionLabelOne> prescriptionLabelOnes = prescriptionLabelOneDao.prescriptionLabelOne(patientExerciseprescription.getFavouriteId());
        if (prescriptionLabelOnes != null && prescriptionLabelOnes.size() > 0){
            for (PrescriptionLabelOne prescriptionLabelOne : prescriptionLabelOnes) {
                List<PrescriptionLabelTwo> prescriptionLabelTwos = prescriptionLabelTwoDao.prescriptionLabelTwo(prescriptionLabelOne.getId());
                if (prescriptionLabelTwos != null && prescriptionLabelTwos.size() > 0){
                    for (PrescriptionLabelTwo prescriptionLabelTwo : prescriptionLabelTwos) {
                        List<PrescriptionLabelThree> prescriptionLabelThrees = prescriptionLabelThreeDao.prescriptionLabelThree(prescriptionLabelTwo.getId());
                        if (prescriptionLabelThrees != null && prescriptionLabelThrees.size() > 0){
                            prescriptionLabelTwo.setLabelThrees(prescriptionLabelThrees);
                        }
                    }
                    prescriptionLabelOne.setLabelTwos(prescriptionLabelTwos);
                }
                if (prescriptionLabelOne.getName().equals("目的")){
                    patientExerciseprescription.setPurposeList(prescriptionLabelOne);
                }
                else if (prescriptionLabelOne.getName().equals("风险等级")){
                    patientExerciseprescription.setRiskLevelList(prescriptionLabelOne);
                }
                else if (prescriptionLabelOne.getName().equals("并发症")){
                    patientExerciseprescription.setComplicationList(prescriptionLabelOne);
                }
                else if (prescriptionLabelOne.getName().equals("肌肉选择")){
                    patientExerciseprescription.setMuscleSelectionList(prescriptionLabelOne);
                }
                else if (prescriptionLabelOne.getName().equals("适用症")){
                    patientExerciseprescription.setApplicableDiseaseList(prescriptionLabelOne);
                }
            }
        }

        List<PrescriptionVideo> prescriptionVideos = prescriptionVideoDao.prescriptionVideoList(patientExerciseprescription.getFavouriteId());
        patientExerciseprescription.setPrescriptionVideoList(prescriptionVideos);

        SysDictItem sysDictItem = sysDictItemDao.queryById(patientExerciseprescription.getClassificationId());
        if (sysDictItem != null){
            patientExerciseprescription.setCategory(sysDictItem.getItemName());
            patientExerciseprescription.setCategoryId(sysDictItem.getId());
        }

        SysDict sysDict = sysDictDao.queryById(sysDictItem.getDictId());
        if (sysDict != null){
            patientExerciseprescription.setParentCategory(sysDict.getDictName());
            patientExerciseprescription.setParentCategoryId(sysDict.getId());
        }

        return patientExerciseprescription;
    }

    /**
     * 修改患者运动处方
     * */
    @Transactional
    @Override
    public int saveCustomMade(PatientExerciseprescription patientExerciseprescription) throws Exception {
        List<PrescriptionVideo> prescriptionVideos = prescriptionVideoDao.prescriptionVideoList(patientExerciseprescription.getFavouriteId());
        if (prescriptionVideos != null && prescriptionVideos.size() > 0){
            for (PrescriptionVideo prescriptionVideo : prescriptionVideos) {
                prescriptionVideoDao.deleteById(prescriptionVideo.getId());
            }
        }
        if (patientExerciseprescription.getPrescriptionVideoList() != null && patientExerciseprescription.getPrescriptionVideoList().size() > 0){
            for (PrescriptionVideo prescriptionVideo : patientExerciseprescription.getPrescriptionVideoList()) {
                PublicVideo publicVideo = publicVideoDao.selectPublicVideo(prescriptionVideo.getPublicVideoId());
                if (publicVideo != null){
                    prescriptionVideo.setPublicVideoName(publicVideo.getPrescriptionVideoName());
                    prescriptionVideo.setPublicVideoUrl(publicVideo.getPrescriptionVideoUrl());
                    prescriptionVideo.setTime(publicVideo.getTime());
                    prescriptionVideo.setBranch(publicVideo.getBranch());
                    prescriptionVideo.setSecond(publicVideo.getSecond());
                    prescriptionVideo.setHealthplanId(patientExerciseprescription.getFavouriteId());
                }

                prescriptionVideo.setClassification("patient");
                int insert = prescriptionVideoDao.insert(prescriptionVideo);
                if (insert <= 0){
                    throw new Exception("修改视频失败：" + prescriptionVideo.getPublicVideoId());
                }
            }
        }
        int insert = patientExerciseprescriptionDao.update(patientExerciseprescription);

        if (patientExerciseprescription.getPurposeList() != null){
            int i = insertPrescriptionLabelOne(patientExerciseprescription.getPurposeList(), patientExerciseprescription.getFavouriteId());
            if (i <= 0){
                throw new Exception("添加目的标签失败");
            }
        }

        if (patientExerciseprescription.getRiskLevelList() != null){
            int i = insertPrescriptionLabelOne(patientExerciseprescription.getRiskLevelList(), patientExerciseprescription.getFavouriteId());
            if (i <= 0){
                throw new Exception("添加风险等级标签失败");
            }
        }

        if (patientExerciseprescription.getComplicationList() != null){
            int i = insertPrescriptionLabelOne(patientExerciseprescription.getComplicationList(), patientExerciseprescription.getFavouriteId());
            if (i <= 0){
                throw new Exception("添加并发症标签失败");
            }
        }

        if (patientExerciseprescription.getMuscleSelectionList() != null){
            int i = insertPrescriptionLabelOne(patientExerciseprescription.getMuscleSelectionList(), patientExerciseprescription.getFavouriteId());
            if (i <= 0){
                throw new Exception("添加肌肉选择标签失败");
            }
        }

        if (patientExerciseprescription.getApplicableDiseaseList() != null){
            int i = insertPrescriptionLabelOne(patientExerciseprescription.getApplicableDiseaseList(), patientExerciseprescription.getFavouriteId());
            if (i <= 0){
                throw new Exception("添加适用症标签失败");
            }
        }

        return insert;
    }

    /**
     * 运动处方总集合赋值
     * */
    public ExercisePrescriptionListVo exercisePrescriptionListVo(List<Exerciseprescription> exerciseprescriptions) throws Exception {
        if (exerciseprescriptions != null && exerciseprescriptions.size() > 0){
            ExercisePrescriptionListVo exercisePrescriptionListVo = new ExercisePrescriptionListVo();

            List<PrescriptionVo> prescriptionVoList = new ArrayList<>();//运动处方一级分类内容集合
            int count = 0;
            for (int i = 0 ; i < 3 ; i++){
                PrescriptionVo prescriptionVo = prescriptionVo(exerciseprescriptions, count);
                prescriptionVoList.add(prescriptionVo);
                count = count + 1;
            }

            exercisePrescriptionListVo.setPrescriptionVoList(prescriptionVoList);

            return exercisePrescriptionListVo;
        }
        else {
            throw new Exception("运动处方没有数据");
        }
    }

    /**
     * 运动处方一级分类内容集合赋值
     * */
    public PrescriptionVo prescriptionVo(List<Exerciseprescription> exerciseprescriptions, int count) throws Exception {
        if (exerciseprescriptions != null && exerciseprescriptions.size() > 0){
            PrescriptionVo prescriptionVo = new PrescriptionVo();
            if (count == 0){//诊断
                prescriptionVo.setPrescriptionName("诊断");
                List<ExerciseVo> exerciseVoList = exerciseVoList(exerciseprescriptions);//运动处方二级分类集合
                prescriptionVo.setExerciseVoList(exerciseVoList);
            }
            else if (count == 1){//运动类别
                prescriptionVo.setPrescriptionName("运动类别");
            }
            else if (count == 2){//身体部位
                prescriptionVo.setPrescriptionName("身体部位");
            }

            return prescriptionVo;
        }
        else {
            throw new Exception("运动处方没有数据");
        }
    }

    /**
     * 运动处方二级分类集合赋值
     * */
    public List<ExerciseVo> exerciseVoList(List<Exerciseprescription> exerciseprescriptions) throws Exception {
        if (exerciseprescriptions != null && exerciseprescriptions.size() > 0){
            List<ExerciseVo> exerciseVoList = new ArrayList<>();

            for (Exerciseprescription exerciseprescription : exerciseprescriptions) {
                Disease disease = diseaseDao.selectDiseaseByDiseaseId(exerciseprescription.getDiseaseId());
                ExerciseVo exerciseVo = new ExerciseVo();
                exerciseVo.setExerciseName(disease.getDiseaseName());
                exerciseVo.setExercisePrescriptionDetailsVoList(exercisePrescriptionDetailsVoList(exerciseprescriptions, disease.getDiseaseId()));
                exerciseVoList.add(exerciseVo);
            }

            return exerciseVoList;
        }
        else {
            throw new Exception("运动处方没有数据");
        }
    }

    /**
     * 运动处方三级名称赋值
     * */
    public List<ExercisePrescriptionDetailsVo> exercisePrescriptionDetailsVoList(List<Exerciseprescription> exerciseprescriptions, Integer diseaseId) throws Exception {
        if (exerciseprescriptions != null && exerciseprescriptions.size() > 0 && diseaseId != null && diseaseId != 0){
            List<ExercisePrescriptionDetailsVo> exercisePrescriptionDetailsVoList = new ArrayList<>();

            for (Exerciseprescription exerciseprescription : exerciseprescriptions) {
                if (exerciseprescription.getDiseaseId() == diseaseId){
                    ExercisePrescriptionDetailsVo exercisePrescriptionDetailsVo = new ExercisePrescriptionDetailsVo();
                    exercisePrescriptionDetailsVo.setPrescriptionId(exerciseprescription.getPrescriptionId());//运动处方主键

                    List<PublicVideo> publicVideos = publicVideoDao.selectPrescriptionvideo(exerciseprescription.getPrescriptionId());
                    List<String> prescriptionvideoList = new ArrayList<>();
                    if (publicVideos != null && publicVideos.size() > 0){
                        for (PublicVideo publicVideo : publicVideos) {
                            prescriptionvideoList.add(publicVideo.getPrescriptionVideoUrl());
                        }
                    }

                    exercisePrescriptionDetailsVo.setPrescriptionVideo(prescriptionvideoList);//运动处方视频路径
                    exercisePrescriptionDetailsVo.setPrescriptionName(exerciseprescription.getPrescriptionName());//运动处方名称
                    exercisePrescriptionDetailsVo.setExerciseStrength(exerciseprescription.getExerciseLevel());//运动强度
                    exercisePrescriptionDetailsVo.setExerciseEverykeep(exerciseprescription.getExerciseEverykeep());//每节时长
                    exercisePrescriptionDetailsVo.setRehabilitationProgramDuration(exerciseprescription.getExerciseCourse());//康复计划时长
                    exercisePrescriptionDetailsVoList.add(exercisePrescriptionDetailsVo);
                }
            }

            return exercisePrescriptionDetailsVoList;
        }
        else {
            throw new Exception("运动处方没有数据");
        }
    }

    /**
     * 计算相差天数
     * */
    public int time(String tmpStart, String tmpEndDate) throws Exception {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dft.parse(tmpStart);
            Date endDate = dft.parse(tmpEndDate);
            Date nextDay = start;
            int i = 0;
            while (nextDay.before(endDate)){
                Calendar cld = Calendar.getInstance();
                cld.setTime(start);
                cld.add(Calendar.DATE, 1);
                start = cld.getTime();
                nextDay = start;
                i++;
            }
            return i;
        } catch (Exception e){
            throw new Exception("日期转换错误");
        }
    }

    /**
     * 添加标签
     * */
    public int insertPrescriptionLabelOne(PrescriptionLabelOne purpose, Integer id) throws Exception {
        purpose.setExercisePrescriptionId(id);
        int insert = prescriptionLabelOneDao.insert(purpose);
        if (insert <= 0){
            throw new Exception("添加第一层失败");
        }

        if (purpose.getLabelTwos() != null && purpose.getLabelTwos().size() > 0){
            for (PrescriptionLabelTwo labelTwo : purpose.getLabelTwos()) {
                labelTwo.setLabelOneId(purpose.getId());
                insert = prescriptionLabelTwoDao.insert(labelTwo);
                if (insert <= 0){
                    throw new Exception("添加第二层失败");
                }

                if (labelTwo.getLabelThrees() != null && labelTwo.getLabelThrees().size() > 0){
                    for (PrescriptionLabelThree labelThree : labelTwo.getLabelThrees()) {
                        labelThree.setLabelTwoId(labelTwo.getId());
                        insert = prescriptionLabelThreeDao.insert(labelThree);
                        if (insert <= 0){
                            throw new Exception("添加的第三层失败");
                        }
                    }
                }
            }
        }
        return insert;
    }
}